package com.yanqu.road.server.cmd.cross.player;

import com.yanqu.road.entity.player.QueryPlayerResult;
import com.yanqu.road.entity.player.UserBadge;
import com.yanqu.road.entity.player.UserDecoration;
import com.yanqu.road.logic.pb.BadgePb;
import com.yanqu.road.logic.pb.DecorationPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.decoration.DecorationProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractServerCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.S_CROSS_TO_QUERY_PLAYER, desc = "跨服查看玩家")
public class CrossQueryPlayerCmd extends AbstractServerCommand {

	private long userId;

	private long serverId;

	private long watchUserId;

	private long watchServerId;

	private QueryPlayerResult queryPlayerResult;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		DecorationProto.CrossQueryPlayerReqMsg reqMsg = DecorationProto.CrossQueryPlayerReqMsg.parseFrom(packet.getBytes());
		userId = reqMsg.getUserId();
		serverId = reqMsg.getServerId();
		watchUserId = reqMsg.getWatchUserId();
		watchServerId = reqMsg.getWatchServerId();
		return true;
	}

	@Override
	public void execute(YanQuMessage packet) throws Exception {
		queryPlayerResult = UserMgr.queryPlayer(watchUserId);
	}

	@Override
	public void sendData(YanQuMessage packet) throws Exception {
		DecorationProto.CrossQueryPlayerRespMsg.Builder respMsg = DecorationProto.CrossQueryPlayerRespMsg.newBuilder();
		respMsg.setUserId(userId);
		respMsg.setServerId(serverId);
		respMsg.setRet(queryPlayerResult.getRet());
		if(0 == queryPlayerResult.getRet()) {
			PlayerProto.PlayerSimpleTempMsg.Builder userMsg = PlayerPb.parsePlayerSimpleTempMsg(queryPlayerResult.getWatchUserInfo());
			if (null != userMsg) {
				respMsg.setUserData(userMsg);
			}
			if (null != queryPlayerResult.getDecorationMap()) {
				long nowTime = System.currentTimeMillis() / 1000;
				for (UserDecoration userDecoration : queryPlayerResult.getDecorationMap().values()) {
					if (userDecoration.getEndTime() > nowTime) {
						DecorationProto.DecorationTempMsg.Builder decorationMsg = DecorationPb.parseDecorationTempMsg(userDecoration);
						if (null != decorationMsg) {
							respMsg.addDecorationList(decorationMsg);
						}
					}
				}
			}
			if (null != queryPlayerResult.getUserBadgeList()) {
				for (UserBadge userBadge : queryPlayerResult.getUserBadgeList()) {
					respMsg.addBadge(BadgePb.parseBadgeTempMsg(userBadge));
				}
			}
			if (queryPlayerResult.getIpLocation() != null) {
				respMsg.setIpAddress(queryPlayerResult.getIpLocation());
			}
		}
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_QUERY_PLAYER_RESP, respMsg);
		GamePlayerMgr.sendPacket(0, pbMsg);
	}
}
